import { Component, OnInit, Input, SimpleChanges, OnChanges, OnDestroy } from '@angular/core';
import { ModelService } from '../../../service/model.service';
import { ModelParamService } from '../../../service/model-param.service';
import { DataSourceService } from '../../../service/data-source/data-source.service';
import { Model } from '../../../entity/model.model';
import { ModelParam } from '../../../entity/model-Param';
import { List } from 'linqts';
import { Tag } from '../../../entity/tag.model';
import { Http } from '@angular/http';
import { DatePipe } from '@angular/common';
import { Device } from "../../../entity/device.model";
import { TagData } from "../../../entity/tag-data.model";
import { SetofvalueService } from "../../../service/setofvalue.service";
import { Setofvalue } from "../../../entity/setofvalue.model";
import { InterceptorService } from '../../../service/interceptor/interceptor-service';



@Component({
  selector: 'app-device-detail',
  templateUrl: './device-detail.component.html',
  styleUrls: ['./device-detail.component.css']
})
export class DeviceDetailComponent implements OnInit, OnChanges, OnDestroy {

  @Input() device: Device;
   //设备编号
  @Input() deviceNumber:Number;
 
  /**
 * 双击的设备详细信息
 */
  // @Input() convergenceBox: ConvergenceBox;

  public model: Model;
  /**
 * 所有型号
 */
  public models: Model[] = [];
  /**
 * 型号参数
 */
  public modelParams: ModelParam[] = [];
  /**
 * 展示的测点种类数据
 */
  public TagDatas:any[] = [];
  public swithTagDatas: TagData[] = [];
  public analogTagDatas: TagData[] = [];
  /**
  * 测点种类
  */
  public setofvalues: Setofvalue[] = [];
  private dataSource: DataSourceService = null;

  public isA:boolean = false;
  public isB:boolean = false;

  constructor(
    private http: InterceptorService,
    private datePipe: DatePipe,
    private modelService: ModelService,
    private modelParamService: ModelParamService,
    private setofvalueService: SetofvalueService, ) {
  }

  ngOnInit() { 
    if(this.deviceNumber==0){
      this.isA = true;
      this.isB = false;
    }else if(this.deviceNumber==1){
      this.isB = true;
      this.isA = false;
    }   
  }
  ngOnChanges(changes: SimpleChanges): void {
    if (changes['device'] != null && changes['device'].currentValue != null) {
      this.loadConvergenceBoxInfo();
    }
  }
  ngOnDestroy(): void {
    if (this.dataSource != null) {
      this.dataSource.dispose();
    }
  }
  private loadConvergenceBoxInfo(): void {

    if (this.dataSource != null) {
      this.dataSource.dispose();
    }
    this.dataSource = new DataSourceService(this.http, this.datePipe);
    this.model = null;
    this.modelParams = [];
    this.swithTagDatas = [];
    this.analogTagDatas = [];
    this.TagDatas = [];
    // const modelGet = this.modelService.getModelByModelId(this.convergenceBox.modelId);
    const modelGet = this.modelService.getModelWithFactory();
    const tagGet = this.modelService.getTagByModelId(this.device.modelId);
    const modelParamGet = this.modelParamService.getModelParamsByModeID(this.device.modelId);
    const setofValueGet = this.setofvalueService.getSetofValuesBykeyID(510);//后添加的
    const dataSource = this.dataSource;
    Promise.all([modelGet, tagGet, modelParamGet, setofValueGet])
      .then(res => {
        if (res[0]) {
          this.models = [...res[0]];
        };
        let index = this.models.findIndex(o => o.modelId == this.device.modelId);
        if (index >= 0) {
          this.model = this.models[index];
        } else {
          this.model = null;
        }

        if (res[2].length > 0) {
          this.modelParams = res[2];
        } else {
          this.modelParams = [];
        }

        if (res[3].length > 0) {
          this.setofvalues = [...res[3]];
        } else {
          this.setofvalues = [];
        }

        if (res[1] != null && res[1].length > 0) {
          const tags = new List(res[1]);
          const tagGroups = tags.GroupBy(group => group.tagKind, map => map);
          const tagKinds = Object.getOwnPropertyNames(tagGroups);
          // this.setofvalues.forEach(setofvalue => {            
            tagKinds.forEach(tagKind => {
              let tagDataAry = [];
              if (tagKind != null) {
                const tagKindOfTags: Tag[] = Object.getOwnPropertyDescriptor(tagGroups, tagKind).value;
                tagKindOfTags.forEach(tag => {
                  const tagData: TagData = this.dataSource.binding([this.device.farmCode, this.device.deviceCode], tag);
                  tagData.tagUnit = tag.tagUnit ? "(" + tag.tagUnit + ")" : tag.tagUnit;   
                  tagDataAry.push(tagData)  
                });
                tagDataAry['name'] = this.setofvalues.filter(o =>o.valueCode == tagKind)[0].valueDisplay;
                this.TagDatas.push(tagDataAry)
              }
            });
          dataSource.begin();
        }
      })
      .catch(err => {
        console.log(err);
      });

  }
}



// WEBPACK FOOTER //
// ./src/app/module/common/device-detail/device-detail.component.ts